#!/bin/sh

set -e

version="1.0"
InstallDir="/opt/Adobe AIR"
VersionDir="$InstallDir/Versions/$version"
ResourceDir="$VersionDir/Resources"
PATH="$ResourceDir/xdg-utils:$PATH"
XDG_UTILS_INSTALL_MODE="system"

if which kde-config >/dev/null 2>&1; then
    :
else
    PATH="$PATH:/opt/kde3/bin:/opt/kde4/bin:/opt/kde/bin"
fi

if [ -z "$XDG_DATA_DIRS" ]; then
    XDG_DATA_DIRS="/usr/share/:/usr/local/share/"

    if xdg_data_dir=`kde-config --prefix 2>/dev/null`; then
        XDG_DATA_DIRS="${XDG_DATA_DIRS}:${xdg_data_dir}/share/"
    fi

    if [ -x /opt/gnome/bin/gnome-open ]; then
        XDG_DATA_DIRS="${XDG_DATA_DIRS}:/opt/gnome/share/"
    fi

    export XDG_DATA_DIRS
fi

export PATH
export XDG_UTILS_INSTALL_MODE

CheckUpdateAlternatives()
{
    env PATH="$PATH:/sbin:/usr/sbin" which update-alternatives >/dev/null 2>&1
}


InstallIcons()
{
    icon_dir="$1"

    for icon_size in 16 22 24 32 48 64 128; do
        xdg-icon-resource install --noupdate --novendor --context apps --size $icon_size "$icon_dir/${icon_size}x${icon_size}/AdobeAIR.png"
        xdg-icon-resource install --noupdate --novendor --theme gnome --context apps --size $icon_size "$icon_dir/${icon_size}x${icon_size}/AdobeAIR.png"
        xdg-icon-resource install --noupdate --novendor --context apps --size $icon_size "$icon_dir/${icon_size}x${icon_size}/air.png"
        xdg-icon-resource install --noupdate --novendor --theme gnome --context apps --size $icon_size "$icon_dir/${icon_size}x${icon_size}/air.png"
        xdg-icon-resource install --noupdate --novendor --context mimetypes --size $icon_size "$icon_dir/${icon_size}x${icon_size}/air.png" 'application-vnd.adobe.air-application-installer-package+zip'
        xdg-icon-resource install --noupdate --novendor --theme gnome --context mimetypes --size $icon_size "$icon_dir/${icon_size}x${icon_size}/air.png" 'application-vnd.adobe.air-application-installer-package+zip'
    done

    xdg-icon-resource forceupdate
}


InstallDesktopFile()
{
    priority="`echo 1.0 | tr -d .`"
    mkdir -p "/opt/Adobe/Flash Player/AddIns/airappinstaller"
    env PATH="/usr/sbin:$PATH" update-alternatives --install "/usr/bin/Adobe AIR Application Installer" "Adobe_AIR_Application_Installer" "$VersionDir/Adobe AIR Application Installer" ${priority} \
        --slave "/usr/share/applications/AdobeAIR.desktop" "AdobeAIR.desktop" "$ResourceDir/support/AdobeAIR.desktop" \
        --slave "/opt/Adobe/Flash Player/AddIns/airappinstaller/airappinstaller" "airappinstaller" "$VersionDir/Resources/airappinstaller" \
        --slave "/opt/Adobe/Flash Player/AddIns/airappinstaller/digest.s" "digest.s" "$VersionDir/Resources/digest.s" || true
}


InstallDesktopFileOld()
{
    rm -f  /usr/share/applications/AdobeAIR.desktop "/usr/bin/Adobe AIR Application Installer"
    ln -s "$ResourceDir/support/AdobeAIR.desktop" /usr/share/applications/AdobeAIR.desktop
    ln -s "$VersionDir/Adobe AIR Application Installer" "/usr/bin/Adobe AIR Application Installer"
}

MergeCertificates()
{
    if [ -n "$1" ] ; then
        lastInstalledVersion="$1"
        currentVersion="2.5.1.17730"
        if dpkg --compare-versions "$lastInstalledVersion" 'lt' "$currentVersion" 2>/dev/null
        then
            if [ -f "/etc/opt/Adobe/certificates/AIR/crypt/config.xml.dpkg-dist" ] ; then
                inputFile="/etc/opt/Adobe/certificates/AIR/crypt/config.xml.dpkg-dist"
            elif [ -f "/etc/opt/Adobe/certificates/crypt/config.xml.dpkg-dist" ] ; then
                inputFile="/etc/opt/Adobe/certificates/crypt/config.xml.dpkg-dist"
                cp -rf /etc/opt/Adobe/certificates/crypt/*.[0-9]* /etc/opt/Adobe/certificates/AIR/crypt/ &>/dev/null
            elif [ -f "/etc/opt/Adobe/certificates/crypt/config.xml" ] ; then
                inputFile="/etc/opt/Adobe/certificates/crypt/config.xml"
                cp -rf /etc/opt/Adobe/certificates/crypt/*.[0-9]* /etc/opt/Adobe/certificates/AIR/crypt/ &>/dev/null
            fi

            if [ -n "${inputFile}" ] ; then 
                "$ResourceDir/aucm" -m -S "${inputFile}" -D "/etc/opt/Adobe/certificates/AIR/crypt/config.xml"
                return $?
            fi
        fi
    fi
    return 0
}


InstallMimeType()
{
    xdg-mime install --novendor "$ResourceDir/support/AdobeAIR.xml"
}


SetAsDefaultApplication()
{
    oldpwd="$PWD"

    cd "$ResourceDir/support"
    xdg-mime default AdobeAIR.desktop 'application/vnd.adobe.air-application-installer-package+zip'

    cd "$oldpwd"
}

## Check if this runtime is the latest of all the installed
GetLatestVersion()
{
    ls -1Sr "$1" 2>/dev/null | (
        while read ver
        do
            if [ -f "$1/$ver/Adobe AIR Application Installer" ]; then
                echo "$ver"
                return 0
            fi
        done

        return 1
    )
}

IsLatestVersion()
{
    sxsVersion="1.0"

    if latest="`GetLatestVersion "$1"`"; then
        test "$latest" = "$sxsVersion"
        return $?
    else
        return 0
    fi
}


case "$1" in
    configure)

    CheckUpdateAlternatives && InstallDesktopFile >/dev/null 2>&1;

    if __TAG_HANDLE_AIR__ "$InstallDir/Versions"; then
        InstallMimeType >/dev/null 2>&1
        SetAsDefaultApplication >/dev/null 2>&1
        InstallIcons "$ResourceDir/support/icons" >/dev/null 2>&1
        CheckUpdateAlternatives || InstallDesktopFile >/dev/null 2>&1
    fi || true

# Nested checking calls, because it's possble that one of the child does not exist but
# we still need to make sure that all the parent nodes are updated if not links
    if [ -h "/var/opt/Adobe AIR/Shared" ]
    then
    	:
    else
        chmod 1777 "/var/opt/Adobe AIR/Shared" 2>/dev/null

	if [ -e "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater" ]
	then
		:
	else
		mkdir -p "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater" 2>/dev/null
	fi

    	if [ -h "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater" ]
        then
		:
	else
		chmod 1777 "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater" 2>/dev/null

    		if [ -e "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater/initialPingback" ]
		then
	    		if [ -h "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater/initialPingback" ]
        	   	then
				:
			else
		    	    	chmod 666 "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater/initialPingback" 2>/dev/null
			fi
    		fi
    	fi
    fi

    if [ -h "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater/updatePingback" -a -e "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater/initialPingback" ]
    then
    	:
    else
    	rm -f "/var/opt/Adobe AIR/Shared/Adobe/AIR/Updater/updatePingback" 2>/dev/null
    fi

    exitStatus=0
    MergeCertificates "$2"
    exitStatus=$?

    if [ "$exitStatus" = "0" ]
    then
      rm -f "/etc/opt/Adobe/certificates/AIR/crypt/config.xml.dpkg-dist"
      rm -f "/etc/opt/Adobe/certificates/crypt/config.xml.dpkg-dist"
    fi

    exit $exitStatus
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

exit 0

